#include "ipc_data.h"

#include <gio/gio.h>
#include <thread>

// 代码主要来自Android
// (非框架设计者不用理会具体实现)

using std::map;
using std::set;
using std::vector;



namespace ipc
{
	static const std::vector<int8_t>		EMPTY_INT8_VECTOR;
	static const std::vector<int16_t>		EMPTY_INT16_VECTOR;
	static const std::vector<int32_t>		EMPTY_INT32_VECTOR;
	static const std::vector<int64_t>		EMPTY_INT64_VECTOR;
	static const std::vector<float>			EMPTY_FLOAT_VECTOR;
	static const std::vector<double>		EMPTY_DOUBLE_VECTOR;
	static const std::vector<std::string>	EMPTY_STRING_VECTOR;
	static const std::string 				EMPTY_STR;
	
} // namespace ipc




namespace ipc
{	
	// 构造 ------------------------------------------------------------------------
	
	Variant::Variant(const Variant& rhs)
	{
		COPY_FUNCS[rhs.mType](this, rhs);	// 内部设置了this->mType
		
	}
	
	Variant& Variant::operator=(const Variant& rhs)
	{
		if (this != &rhs)
		{
			CLEAR_FUNCS[mType](this);
			COPY_FUNCS[rhs.mType](this, rhs); // 内部设置了this->mType			
		}
		return *this;		
	}
	
	Variant::Variant(Variant&& rhs)
	{
		MOVE_FUNCS[rhs.mType](this, rhs);	// 内部设置了this->mType
		rhs.mType = TYPE_INT8;
	}	
	
	Variant& Variant::operator=(Variant&& rhs)
	{
		if (this != &rhs)
		{
			CLEAR_FUNCS[mType](this);
			MOVE_FUNCS[rhs.mType](this, rhs); // 内部设置了this->mType			
			rhs.mType = TYPE_INT8;
		}
		return *this;
	}
	
	bool operator==(const Variant& lhs, const Variant& rhs)
	{
		return lhs.mType == rhs.mType && Variant::EQUAL_FUNCS[lhs.mType](lhs.mData, rhs.mData);
	}
	
	bool operator!=(const Variant& lhs, const Variant& rhs)
	{
		return !(rhs == rhs);
	}

} // namespace ipc









namespace ipc
{
	// 清理 ------------------------------------------------------------------------

	const Variant::ClearFunc Variant::CLEAR_FUNCS[TYPE_MAX] = {
		clearNone,		// 8
		clearNone,      // 16
		clearNone,      // 32
		clearNone,      // 64
		clearNone,      // float
		clearNone,      // double
		clearString,

		clearInt8Vector,
		clearInt16Vector,
		clearInt32Vector,
		clearInt64Vector,
		clearFloatVector,
		clearDoubleVector,
		clearStringVector
	};

	void Variant::clearNone(Variant* v)
	{
		(void)v;
	}

	void Variant::clearString(Variant* v)
	{
		v->mData.mString.~basic_string();
	}

	void Variant::clearInt8Vector(Variant* v)
	{
		v->mData.mInt8Vector.~vector<int8_t>();
	}

	void Variant::clearInt16Vector(Variant* v)
	{
		v->mData.mInt16Vector.~vector<int16_t>();
	}

	void Variant::clearInt32Vector(Variant* v)
	{
		v->mData.mInt32Vector.~vector<int32_t>();
	}

	void Variant::clearInt64Vector(Variant* v)
	{
		v->mData.mInt64Vector.~vector<int64_t>();
	}

	void Variant::clearFloatVector(Variant* v)
	{
		v->mData.mFloatVector.~vector<float>();
	}

	void Variant::clearDoubleVector(Variant* v)
	{
		v->mData.mDoubleVector.~vector<double>();
	}

	void Variant::clearStringVector(Variant* v)
	{
		v->mData.mStringVector.~vector<std::string>();
	}

} // namespace ipc










namespace ipc
{
	// 复制 ------------------------------------------------------------------------
	
	const Variant::CopyFunc Variant::COPY_FUNCS[TYPE_MAX] = {
		copyInt64,	// 8
		copyInt64,	// 16
		copyInt64,	// 32
		copyInt64,	// 64
		copyInt64,	// float
		copyInt64,	// double
		copyString,

		copyInt8Vector,
		copyInt16Vector,
		copyInt32Vector,
		copyInt64Vector,
		copyFloatVector,
		copyDoubleVector,
		copyStringVector
	};
	
	void Variant::copyInt64(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt64(rhs.mData.mInt64);
	}
	
	void Variant::copyString(Variant* lhs, const Variant& rhs)
	{
		lhs->setString(rhs.mData.mString);
	}
		
	void Variant::copyInt8Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt8Vector(rhs.mData.mInt8Vector);
	}
	
	void Variant::copyInt16Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt16Vector(rhs.mData.mInt16Vector);
	}
	
	void Variant::copyInt32Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt32Vector(rhs.mData.mInt32Vector);
	}
	
	void Variant::copyInt64Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt64Vector(rhs.mData.mInt64Vector);
	}
	
	void Variant::copyFloatVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setFloatVector(rhs.mData.mFloatVector);
	}
	
	void Variant::copyDoubleVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setDoubleVector(rhs.mData.mDoubleVector);
	}
	
	void Variant::copyStringVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setStringVector(rhs.mData.mStringVector);
	}
	
} // namespace ipc










namespace ipc
{
	// 移动 ------------------------------------------------------------------------
	
	const Variant::MoveFunc Variant::MOVE_FUNCS[TYPE_MAX] = {
		copyInt64,	// 8
		copyInt64,	// 16
		copyInt64,	// 32
		copyInt64,	// 64
		copyInt64,	// float
		copyInt64,	// double
		moveString,

		moveInt8Vector,
		moveInt16Vector,
		moveInt32Vector,
		moveInt64Vector,
		moveFloatVector,
		moveDoubleVector,
		moveStringVector
	};	
	
	void Variant::moveString(Variant* lhs, const Variant& rhs)
	{
		lhs->mType = TYPE_STRING;
		new(&lhs->mData.mString) std::string(std::move(rhs.mData.mString));
	}
		
	void Variant::moveInt8Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt8Vector(std::move(rhs.mData.mInt8Vector));
	}
	
	void Variant::moveInt16Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt16Vector(std::move(rhs.mData.mInt16Vector));
	}
	
	void Variant::moveInt32Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt32Vector(std::move(rhs.mData.mInt32Vector));
	}
	
	void Variant::moveInt64Vector(Variant* lhs, const Variant& rhs)
	{
		lhs->setInt64Vector(std::move(rhs.mData.mInt64Vector));
	}
	
	void Variant::moveFloatVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setFloatVector(std::move(rhs.mData.mFloatVector));
	}
	
	void Variant::moveDoubleVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setDoubleVector(std::move(rhs.mData.mDoubleVector));
	}
	
	void Variant::moveStringVector(Variant* lhs, const Variant& rhs)
	{
		lhs->setStringVector(std::move(rhs.mData.mStringVector));
	}
	
	
} // namespace ipc










namespace ipc
{
	// 比较 ------------------------------------------------------------------------
	
	const Variant::EqualFunc Variant::EQUAL_FUNCS[TYPE_MAX] = {
		equalInt8,	
		equalInt16,	
		equalInt32,	
		equalInt64,	
		equalFloat,	
		equalDouble,
		equalString,

		equalInt8Vector,	
		equalInt16Vector,	
		equalInt32Vector,	
		equalInt64Vector,	
		equalFloatVector,	
		equalDoubleVector,
		equalStringVector,
	};
	
	bool Variant::equalInt8(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt8 == rhs.mInt8;
	}
	
	bool Variant::equalInt16(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt16 == rhs.mInt16;
	}
	
	bool Variant::equalInt32(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt32 == rhs.mInt32;
	}
	
	bool Variant::equalInt64(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt64 == rhs.mInt64;
	}
	
	bool Variant::equalFloat(const Union& lhs, const Union& rhs)
	{
		return lhs.mFloat == rhs.mFloat;
	}
	
	bool Variant::equalDouble(const Union& lhs, const Union& rhs)
	{
		return lhs.mDouble == rhs.mDouble;
	}
	
	bool Variant::equalString(const Union& lhs, const Union& rhs)
	{
		return lhs.mString == rhs.mString;
	}
	
	
	bool Variant::equalInt8Vector(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt8Vector == rhs.mInt8Vector;
	}
	
	bool Variant::equalInt16Vector(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt16Vector == rhs.mInt16Vector;
	}
	
	bool Variant::equalInt32Vector(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt32Vector == rhs.mInt32Vector;
	}
	
	bool Variant::equalInt64Vector(const Union& lhs, const Union& rhs)
	{
		return lhs.mInt64Vector == rhs.mInt64Vector;
	}
	
	bool Variant::equalFloatVector(const Union& lhs, const Union& rhs)
	{
		return lhs.mFloatVector == rhs.mFloatVector;
	}
	
	bool Variant::equalDoubleVector(const Union& lhs, const Union& rhs)
	{
		return lhs.mDoubleVector == rhs.mDoubleVector;
	}
	
	bool Variant::equalStringVector(const Union& lhs, const Union& rhs)
	{
		return lhs.mStringVector == rhs.mStringVector;
	}
	
} // namespace ipc









namespace ipc
{
	// 写入 ------------------------------------------------------------------------
	
	const Variant::WriteFunc Variant::WRITE_FUNCS[TYPE_MAX] = {
		writeInt8,	
		writeInt16,	
		writeInt32,	
		writeInt64,	
		writeFloat,	
		writeDouble,
		writeString,

		writeInt8Vector,	
		writeInt16Vector,	
		writeInt32Vector,	
		writeInt64Vector,	
		writeFloatVector,	
		writeDoubleVector,
		writeStringVector,
	};
	
	status_t Variant::writeInt8(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt8(v.mData.mInt8);
	}
	
	status_t Variant::writeInt16(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt16(v.mData.mInt16);
	}
	
	status_t Variant::writeInt32(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt32(v.mData.mInt32);
	}
	
	status_t Variant::writeInt64(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt64(v.mData.mInt64);
	}
		
	status_t Variant::writeFloat(Parcel* parcel, const Variant& v)
	{
		return parcel->writeFloat(v.mData.mFloat);
	}
	
	status_t Variant::writeDouble(Parcel* parcel, const Variant& v)
	{
		return parcel->writeDouble(v.mData.mDouble);
	}
	
	status_t Variant::writeString(Parcel* parcel, const Variant& v)
	{
		return parcel->writeString(v.mData.mString);
	}
	
	
	status_t Variant::writeInt8Vector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt8Vector(v.mData.mInt8Vector);
	}
	
	status_t Variant::writeInt16Vector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt16Vector(v.mData.mInt16Vector);
	}
	
	status_t Variant::writeInt32Vector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt32Vector(v.mData.mInt32Vector);
	}
	
	status_t Variant::writeInt64Vector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeInt64Vector(v.mData.mInt64Vector);
	}
	
	status_t Variant::writeFloatVector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeFloatVector(v.mData.mFloatVector);
	}
	
	status_t Variant::writeDoubleVector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeDoubleVector(v.mData.mDoubleVector);
	}
	
	status_t Variant::writeStringVector(Parcel* parcel, const Variant& v)
	{
		return parcel->writeStringVector(v.mData.mStringVector);
	}
	
} // namespace ipc	
	
	
	
	

namespace ipc
{
	// 读取 ------------------------------------------------------------------------
	
	const Variant::ReadFunc Variant::READ_FUNCS[TYPE_MAX] = {
		readInt8,	
		readInt16,	
		readInt32,	
		readInt64,	
		readFloat,	
		readDouble,
		readString,

		readInt8Vector,	
		readInt16Vector,	
		readInt32Vector,	
		readInt64Vector,	
		readFloatVector,	
		readDoubleVector,
		readStringVector,
	};
	
	status_t Variant::readInt8(Variant& v, const Parcel* parcel)
	{
		int8_t value;
		status_t ret = parcel->readInt8(&value);
		v.setInt8(value);
		return ret;
	}
	
	status_t Variant::readInt16(Variant& v, const Parcel* parcel)
	{
		int16_t value;
		status_t ret = parcel->readInt16(&value);
		v.setInt16(value);
		return ret;
	}
	
	status_t Variant::readInt32(Variant& v, const Parcel* parcel)
	{
		int32_t value;
		status_t ret = parcel->readInt32(&value);
		v.setInt32(value);
		return ret;
	}
	
	status_t Variant::readInt64(Variant& v, const Parcel* parcel)
	{
		int64_t value;
		status_t ret = parcel->readInt64(&value);
		v.setInt64(value);
		return ret;
	}
	
	status_t Variant::readFloat(Variant& v, const Parcel* parcel)
	{
		float value;
		status_t ret = parcel->readFloat(&value);
		v.setFloat(value);
		return ret;
	}
	
	status_t Variant::readDouble(Variant& v, const Parcel* parcel)
	{
		double value;
		status_t ret = parcel->readDouble(&value);
		v.setDouble(value);
		return ret;
	}
	
	status_t Variant::readString(Variant& v, const Parcel* parcel)
	{
		std::string value;
		status_t ret = parcel->readString(&value);
		v.setString(value);
		return ret;
	}
	
	
	status_t Variant::readInt8Vector(Variant& v, const Parcel* parcel)
	{
		std::vector<int8_t> value;
		status_t ret = parcel->readInt8Vector(&value);
		v.setInt8Vector(std::move(value));
		return ret;
	}
	
	status_t Variant::readInt16Vector(Variant& v, const Parcel* parcel)
	{
		std::vector<int16_t> value;
		status_t ret = parcel->readInt16Vector(&value);
		v.setInt16Vector(std::move(value));
		return ret;
	}
	
	status_t Variant::readInt32Vector(Variant& v, const Parcel* parcel)
	{
		std::vector<int32_t> value;
		status_t ret = parcel->readInt32Vector(&value);
		v.setInt32Vector(std::move(value));
		return ret;
	}
	
	status_t Variant::readInt64Vector(Variant& v, const Parcel* parcel)
	{
		std::vector<int64_t> value;
		status_t ret = parcel->readInt64Vector(&value);
		v.setInt64Vector(std::move(value));
		return ret;
	}
	
	status_t Variant::readFloatVector(Variant& v, const Parcel* parcel)
	{
		std::vector<float> value;
		status_t ret = parcel->readFloatVector(&value);
		v.setFloatVector(std::move(value));
		return ret;
	}
	
	status_t Variant::readDoubleVector(Variant& v, const Parcel* parcel)
	{
		std::vector<double> value;
		status_t ret = parcel->readDoubleVector(&value);
		v.setDoubleVector(std::move(value));
		return ret;
	}
	
	status_t Variant::readStringVector(Variant& v, const Parcel* parcel)
	{
		std::vector<std::string> value;
		status_t ret = parcel->readStringVector(&value);
		v.setStringVector(std::move(value));
		return ret;
	}	
	
} // namespace ipc


	
	
	
namespace ipc
{
	// --------------------------------------------------------------------------
	

	Variant::~Variant()
	{
		CLEAR_FUNCS[mType](this);
	}


	void Variant::setInt8(int8_t value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_INT8;
		mData.mInt8 = value;
	}

	void Variant::setInt16(int16_t value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_INT16;
		mData.mInt16 = value;
	}

	void Variant::setInt32(int32_t value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_INT32;
		mData.mInt32 = value;
	}

	void Variant::setInt64(int64_t value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_INT64;
		mData.mInt64 = value;
	}

	void Variant::setFloat(float value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_FLOAT;
		mData.mFloat = value;
	}

	void Variant::setDouble(double value)
	{
		CLEAR_FUNCS[mType](this);
		mType = TYPE_DOUBLE;
		mData.mDouble = value;
	}

	void Variant::setString(const std::string& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_STRING)
		{
			mType = TYPE_STRING;
			new(&mData.mString) std::string(value);
		}
		else
		{
			mData.mString = value;
		}
	}

	// --------------------------------------------------------------------------

	void Variant::setInt8Vector(const std::vector<int8_t>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT8_VECTOR)
		{
			mType = TYPE_INT8_VECTOR;
			new(&mData.mInt8Vector) std::vector<int8_t>(value);
		}
		else
		{
			mData.mInt8Vector = value;
		}
	}

	void Variant::setInt16Vector(const std::vector<int16_t>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT8_VECTOR)
		{
			mType = TYPE_INT16_VECTOR;
			new(&mData.mInt16Vector) std::vector<int16_t>(value);
		}
		else
		{
			mData.mInt16Vector = value;
		}
	}

	void Variant::setInt32Vector(const std::vector<int32_t>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT32_VECTOR)
		{
			mType = TYPE_INT32_VECTOR;
			new(&mData.mInt32Vector) std::vector<int32_t>(value);
		}
		else
		{
			mData.mInt32Vector = value;
		}
	}

	void Variant::setInt64Vector(const std::vector<int64_t>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT64_VECTOR)
		{
			mType = TYPE_INT64_VECTOR;
			new(&mData.mInt64Vector) std::vector<int64_t>(value);
		}
		else
		{
			mData.mInt64Vector = value;
		}
	}

	void Variant::setFloatVector(const std::vector<float>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_FLOAT_VECTOR)
		{
			mType = TYPE_FLOAT_VECTOR;
			new(&mData.mFloatVector) std::vector<float>(value);
		}
		else
		{
			mData.mFloatVector = value;
		}
	}

	void Variant::setDoubleVector(const std::vector<double>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_DOUBLE_VECTOR)
		{
			mType = TYPE_DOUBLE_VECTOR;
			new(&mData.mDoubleVector) std::vector<double>(value);
		}
		else
		{
			mData.mDoubleVector = value;
		}
	}

	void Variant::setStringVector(const std::vector<std::string>& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_STRING_VECTOR)
		{
			mType = TYPE_STRING_VECTOR;
			new(&mData.mStringVector) std::vector<std::string>(value);
		}
		else
		{
			mData.mStringVector = value;
		}
	}

	// --------------------------------------------------------------------------

	void Variant::setInt8Vector(std::vector<int8_t>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT8_VECTOR)
		{
			mType = TYPE_INT8_VECTOR;
			new(&mData.mInt8Vector) std::vector<int8_t>(std::move(value));
		}
		else
		{
			mData.mInt8Vector = std::move(value);
		}
	}

	void Variant::setInt16Vector(std::vector<int16_t>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT8_VECTOR)
		{
			mType = TYPE_INT16_VECTOR;
			new(&mData.mInt16Vector) std::vector<int16_t>(std::move(value));
		}
		else
		{
			mData.mInt16Vector = std::move(value);
		}
	}

	void Variant::setInt32Vector(std::vector<int32_t>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT32_VECTOR)
		{
			mType = TYPE_INT32_VECTOR;
			new(&mData.mInt32Vector) std::vector<int32_t>(std::move(value));
		}
		else
		{
			mData.mInt32Vector = std::move(value);
		}
	}

	void Variant::setInt64Vector(std::vector<int64_t>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_INT64_VECTOR)
		{
			mType = TYPE_INT64_VECTOR;
			new(&mData.mInt64Vector) std::vector<int64_t>(std::move(value));
		}
		else
		{
			mData.mInt64Vector = std::move(value);
		}
	}

	void Variant::setFloatVector(std::vector<float>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_FLOAT_VECTOR)
		{
			mType = TYPE_FLOAT_VECTOR;
			new(&mData.mFloatVector) std::vector<float>(std::move(value));
		}
		else
		{
			mData.mFloatVector = std::move(value);
		}
	}

	void Variant::setDoubleVector(std::vector<double>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_DOUBLE_VECTOR)
		{
			mType = TYPE_DOUBLE_VECTOR;
			new(&mData.mDoubleVector) std::vector<double>(std::move(value));
		}
		else
		{
			mData.mDoubleVector = std::move(value);
		}
	}

	void Variant::setStringVector(std::vector<std::string>&& value)
	{
		CLEAR_FUNCS[mType](this);
		if (mType != TYPE_STRING_VECTOR)
		{
			mType = TYPE_STRING_VECTOR;
			new(&mData.mStringVector) std::vector<std::string>(std::move(value));
		}
		else
		{
			mData.mStringVector = std::move(value);
		}
	}

	// --------------------------------------------------------------------------


	int8_t Variant::getInt8(int8_t defaultValue) const
	{
		return mType == TYPE_INT8 ? mData.mInt8 : defaultValue;
	}

	int16_t Variant::getInt16(int8_t defaultValue) const
	{
		return mType == TYPE_INT16 ? mData.mInt16 : defaultValue;
	}

	int32_t Variant::getInt32(int8_t defaultValue) const
	{
		return mType == TYPE_INT32 ? mData.mInt32 : defaultValue;
	}

	int64_t Variant::getInt64(int8_t defaultValue) const
	{
		return mType == TYPE_INT64 ? mData.mInt64 : defaultValue;
	}

	float Variant::getFloat(int8_t defaultValue) const
	{
		return mType == TYPE_FLOAT ? mData.mFloat : defaultValue;
	}

	double Variant::getDouble(int8_t defaultValue) const
	{
		return mType == TYPE_DOUBLE ? mData.mDouble : defaultValue;
	}

	std::string Variant::getString() const
	{
		return mType == TYPE_STRING ? mData.mString : EMPTY_STR;
	}

	std::string Variant::getString(const std::string& defaultValue) const
	{
		return mType == TYPE_STRING ? mData.mString : defaultValue;
	}


	// --------------------------------------------------------------------------

	std::vector<int8_t> Variant::getInt8Vector() const
	{
		return mType == TYPE_INT8_VECTOR ? mData.mInt8Vector : std::vector<int8_t>();
	}

	std::vector<int16_t> Variant::getInt16Vector() const
	{
		return mType == TYPE_INT16_VECTOR ? mData.mInt16Vector : std::vector<int16_t>();
	}

	std::vector<int32_t> Variant::getInt32Vector() const
	{
		return mType == TYPE_INT32_VECTOR ? mData.mInt32Vector : std::vector<int32_t>();
	}

	std::vector<int64_t> Variant::getInt64Vector() const
	{
		return mType == TYPE_INT64_VECTOR ? mData.mInt64Vector : std::vector<int64_t>();
	}

	std::vector<float> Variant::getFloatVector() const
	{
		return mType == TYPE_FLOAT_VECTOR ? mData.mFloatVector : std::vector<float>();
	}

	std::vector<double> Variant::getDoubleVector() const
	{
		return mType == TYPE_DOUBLE_VECTOR ? mData.mDoubleVector : std::vector<double>();
	}

	std::vector<std::string> Variant::getStringVector() const
	{
		return mType == TYPE_STRING_VECTOR ? mData.mStringVector : std::vector<std::string>();
	}


	// --------------------------------------------------------------------------

	const std::vector<int8_t>& Variant::getInt8VectorRef() const
	{
		return mType == TYPE_INT8_VECTOR ? mData.mInt8Vector : EMPTY_INT8_VECTOR;
	}

	const std::vector<int16_t>& Variant::getInt16VectorRef() const
	{
		return mType == TYPE_INT16_VECTOR ? mData.mInt16Vector : EMPTY_INT16_VECTOR;
	}

	const std::vector<int32_t>& Variant::getInt32VectorRef() const
	{
		return mType == TYPE_INT32_VECTOR ? mData.mInt32Vector : EMPTY_INT32_VECTOR;
	}

	const std::vector<int64_t>& Variant::getInt64VectorRef() const
	{
		return mType == TYPE_INT64_VECTOR ? mData.mInt64Vector : EMPTY_INT64_VECTOR;
	}

	const std::vector<float>& Variant::getFloatVectorRef() const
	{
		return mType == TYPE_FLOAT_VECTOR ? mData.mFloatVector : EMPTY_FLOAT_VECTOR;
	}

	const std::vector<double>& Variant::getDoubleVectorRef() const
	{
		return mType == TYPE_DOUBLE_VECTOR ? mData.mDoubleVector : EMPTY_DOUBLE_VECTOR;
	}

	const std::vector<std::string>& Variant::getStringVectorRef() const
	{
		return mType == TYPE_STRING_VECTOR ? mData.mStringVector : EMPTY_STRING_VECTOR;
	}
	
}	// namespace ipc






//#include <stdio.h>
#ifndef ALOGE
#define ALOGE(...) // {printf(__VA_ARGS__); printf("\n");}
#endif





namespace ipc 
{	
	enum 
	{
		// Keep them in sync with BUNDLE_MAGIC* in frameworks/base/core/java/android/os/BaseBundle.java.
		BUNDLE_MAGIC = 0x4C444E42,
		BUNDLE_MAGIC_NATIVE = 0x4C444E44,
	};	

#define RETURN_IF_FAILED(calledOnce)                                     \
	{                                                                    \
		status_t returnStatus = calledOnce;                              \
		if (returnStatus) {                                              \
			ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
			return returnStatus;                                         \
		}                                                                \
	}


	Bundle::Bundle(Bundle&& bundle)
		: mMap(std::move(bundle.mMap))
	{
	}

	Bundle& Bundle::operator=(Bundle&& rhs)
	{
		if (this != &rhs)
		{
			mMap = std::move(rhs.mMap);
		}
		return *this;
	}
	

	status_t Bundle::writeToParcel(Parcel* parcel) const
	{
		/*
		 * Keep implementation in sync with writeToParcelInner() in
		 * frameworks/base/core/java/android/os/BaseBundle.java.
		 */

		// Special case for empty bundles.
		if (empty()) 
		{
			RETURN_IF_FAILED(parcel->writeInt32(0));
			return NO_ERROR;
		}

		// 数据长度， MAGIC, 数据
		size_t length_pos = parcel->dataPosition();
		RETURN_IF_FAILED(parcel->writeInt32(1)); // dummy, will hold length
		RETURN_IF_FAILED(parcel->writeInt32(BUNDLE_MAGIC_NATIVE));

		size_t start_pos = parcel->dataPosition();
		RETURN_IF_FAILED(writeToParcelInner(parcel));
		size_t end_pos = parcel->dataPosition();

		// Backpatch length. This length value includes the length header.
		parcel->setDataPosition(length_pos);
		size_t length = end_pos - start_pos;
		if (length > (size_t)std::numeric_limits<int32_t>::max()) 
		{
			ALOGE("Parcel length (%zu) too large to store in 32-bit signed int", length);
			return BAD_VALUE;
		}
		RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(length)));
		parcel->setDataPosition(end_pos);
		return NO_ERROR;
	}

	status_t Bundle::readFromParcel(const Parcel* parcel)
	{
		parcel->setDataPosition(0); // Important!!!!
		/*
		 * Keep implementation in sync with readFromParcelInner() in
		 * frameworks/base/core/java/android/os/BaseBundle.java.
		 */
		int32_t length = parcel->readInt32();
		if (length < 0) 
		{
			ALOGE("Bad length in parcel: %d", length);
			return UNEXPECTED_NULL;
		}

		return readFromParcelInner(parcel, static_cast<size_t>(length));
	}

	bool Bundle::empty() const
	{
		return size() == 0u;
	}

	size_t Bundle::size() const
	{
		return mMap.size();
	}

	size_t Bundle::erase(const std::string& key)
	{
		return mMap.erase(key);
	}


	// 设置相关 -------------------------------------------------------------------------------------------------

	Bundle& Bundle::setInt8(const std::string& key, const int8_t value)
	{
		mMap[key].setInt8(value);
		return *this;
	}

	Bundle& Bundle::setInt16(const std::string& key, const int16_t value)
	{
		mMap[key].setInt16(value);
		return *this;
	}

	Bundle& Bundle::setInt32(const std::string& key, const int32_t value)
	{
		mMap[key].setInt32(value);
		return *this;
	}

	Bundle& Bundle::setInt64(const std::string& key, const int64_t value)
	{
		mMap[key].setInt64(value);
		return *this;
	}

	Bundle& Bundle::setFloat(const std::string& key, const float value)
	{ 
		mMap[key].setFloat(value);
		return *this;
	}

	Bundle& Bundle::setDouble(const std::string& key, const double value)
	{
		mMap[key].setDouble(value);
		return *this;
	}

	Bundle& Bundle::setString(const std::string& key, const std::string& value)
	{
		mMap[key].setString(value);
		return *this;
	}

	Bundle& Bundle::setInt8Vector(const std::string& key, const vector<int8_t>& value)
	{
		mMap[key].setInt8Vector(value);
		return *this;
	}

	Bundle& Bundle::setInt16Vector(const std::string& key, const vector<int16_t>& value)
	{
		mMap[key].setInt16Vector(value);
		return *this;
	}

	Bundle& Bundle::setInt32Vector(const std::string& key, const vector<int32_t>& value)
	{
		mMap[key].setInt32Vector(value);
		return *this;
	}

	Bundle& Bundle::setInt64Vector(const std::string& key, const vector<int64_t>& value)
	{
		mMap[key].setInt64Vector(value);
		return *this;
	}

	Bundle& Bundle::setFloatVector(const std::string& key, const vector<float>& value)
	{
		mMap[key].setFloatVector(value);
		return *this;
	}

	Bundle& Bundle::setDoubleVector(const std::string& key, const vector<double>& value)
	{
		mMap[key].setDoubleVector(value);
		return *this;
	}

	Bundle& Bundle::setStringVector(const std::string& key, const vector<std::string>& value)
	{
		mMap[key].setStringVector(value);
		return *this;
	}

	// 设置相关(右值版本,防止大规模变量拷贝) ----------------------------------------------------------------------------------------

	Bundle& Bundle::setInt8Vector(const std::string& key, vector<int8_t>&& value)
	{
		mMap[key].setInt8Vector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setInt16Vector(const std::string& key, vector<int16_t>&& value)
	{
		mMap[key].setInt16Vector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setInt32Vector(const std::string& key, vector<int32_t>&& value)
	{
		mMap[key].setInt32Vector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setInt64Vector(const std::string& key, vector<int64_t>&& value)
	{
		mMap[key].setInt64Vector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setFloatVector(const std::string& key, vector<float>&& value)
	{
		mMap[key].setFloatVector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setDoubleVector(const std::string& key, vector<double>&& value)
	{
		mMap[key].setDoubleVector(std::move(value));
		return *this;
	}

	Bundle& Bundle::setStringVector(const std::string& key, vector<std::string>&& value)
	{
		mMap[key].setStringVector(std::move(value));
		return *this;
	}

	// 获取相关 -------------------------------------------------------------------------------------------------

	int8_t Bundle::getInt8(const std::string& key, int8_t defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT8)
		{
			return it->second.getInt8();
		}
		return defaultValue;
	}

	int16_t Bundle::getInt16(const std::string& key, int16_t defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT16)
		{
			return it->second.getInt16();
		}
		return defaultValue;
	}

	int32_t Bundle::getInt32(const std::string& key, int32_t defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT32)
		{
			return it->second.getInt32();
		}
		return defaultValue;
	}

	int64_t Bundle::getInt64(const std::string& key, int64_t defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT64)
		{
			return it->second.getInt64();
		}
		return defaultValue;
	}

	float Bundle::getFloat(const std::string& key, float defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_FLOAT)
		{
			return it->second.getFloat();
		}
		return defaultValue;
	}

	double Bundle::getDouble(const std::string& key, double defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_DOUBLE)
		{
			return it->second.getDouble();
		}
		return defaultValue;
	}
	
	std::string Bundle::getString(const std::string& key) const
	{
		return getString(key, EMPTY_STR);
	}

	std::string Bundle::getString(const std::string& key, const std::string& defaultValue) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_STRING)
		{
			return it->second.getString();
		}
		return defaultValue;
	}
	
	
	// -------------------------------------------------------------------------------------------------

	std::vector<int8_t> Bundle::getInt8Vector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT8_VECTOR)
		{
			return it->second.getInt8Vector();
		}
		return EMPTY_INT8_VECTOR;
	}

	std::vector<int16_t> Bundle::getInt16Vector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT16_VECTOR)
		{
			return it->second.getInt16Vector();
		}
		return EMPTY_INT16_VECTOR;
	}

	std::vector<int32_t> Bundle::getInt32Vector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT32_VECTOR)
		{
			return it->second.getInt32Vector();
		}
		return EMPTY_INT32_VECTOR;
	}

	std::vector<int64_t> Bundle::getInt64Vector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT64_VECTOR)
		{
			return it->second.getInt64Vector();
		}
		return EMPTY_INT64_VECTOR;
	}

	std::vector<float> Bundle::getFloatVector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_FLOAT_VECTOR)
		{
			return it->second.getFloatVector();
		}
		return EMPTY_FLOAT_VECTOR;
	}

	std::vector<double> Bundle::getDoubleVector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_DOUBLE_VECTOR)
		{
			return it->second.getDoubleVector();
		}
		return EMPTY_DOUBLE_VECTOR;
	}

	std::vector<std::string> Bundle::getStringVector(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_STRING_VECTOR)
		{
			return it->second.getStringVector();
		}
		return EMPTY_STRING_VECTOR;
	}

	// -------------------------------------------------------------------------------------------------
	
	
	const std::vector<int8_t>& Bundle::getInt8VectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT8_VECTOR)
		{
			return it->second.getInt8VectorRef();
		}
		return EMPTY_INT8_VECTOR;
	}

	const std::vector<int16_t>& Bundle::getInt16VectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT16_VECTOR)
		{
			return it->second.getInt16VectorRef();
		}
		return EMPTY_INT16_VECTOR;
	}

	const std::vector<int32_t>& Bundle::getInt32VectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT32_VECTOR)
		{
			return it->second.getInt32VectorRef();
		}
		return EMPTY_INT32_VECTOR;
	}

	const std::vector<int64_t>& Bundle::getInt64VectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_INT64_VECTOR)
		{
			return it->second.getInt64VectorRef();
		}
		return EMPTY_INT64_VECTOR;
	}

	const std::vector<float>& Bundle::getFloatVectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_FLOAT_VECTOR)
		{
			return it->second.getFloatVectorRef();
		}
		return EMPTY_FLOAT_VECTOR;
	}

	const std::vector<double>& Bundle::getDoubleVectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_DOUBLE_VECTOR)
		{
			return it->second.getDoubleVectorRef();
		}
		return EMPTY_DOUBLE_VECTOR;
	}

	const std::vector<std::string>& Bundle::getStringVectorRef(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end() && it->second.getType() == Variant::TYPE_STRING_VECTOR)
		{
			return it->second.getStringVectorRef();
		}
		return EMPTY_STRING_VECTOR;
	}
	

	// 键相关 ---------------------------------------------------------------------------------------------------

	set<std::string> Bundle::getKeySet() const
	{
		set<std::string> keys;
		for (const auto& kv : mMap) 
		{
			keys.emplace(kv.first);
		}
		return keys;
	}
	
	// 获取类型
	int Bundle::getType(const std::string& key) const
	{
		auto it = mMap.find(key);
		if (it != mMap.end())
		{
			return it->second.getType();
		}
		return -1;
	}
	
	bool operator==(const Bundle& lhs, const Bundle& rhs)
	{
		return lhs.mMap == rhs.mMap;
	}
	
	bool operator!=(const Bundle& lhs, const Bundle& rhs)
	{
		return lhs.mMap != rhs.mMap;
	}
	

	// 序列化/反序列化相关 ----------------------------------------------------------------------------------------

	status_t Bundle::writeToParcelInner(Parcel* parcel) const
	{
		/*
		 * To keep this implementation in sync with writeArrayMapInternal() in
		 * frameworks/base/core/java/android/os/Parcel.java, the number of key
		 * value pairs must be written into the parcel before writing the key-value
		 * pairs themselves.
		 */
		size_t num_entries = size();
		if (num_entries > (size_t)std::numeric_limits<int32_t>::max()) {
			ALOGE("The size of this Bundle (%zu) too large to store in 32-bit signed int",
				num_entries);
			return BAD_VALUE;
		}
		RETURN_IF_FAILED(parcel->writeInt32(static_cast<int32_t>(num_entries)));
		
		for (const auto& kv : mMap) 
		{
			RETURN_IF_FAILED(parcel->writeString(kv.first));
			RETURN_IF_FAILED(parcel->writeInt32(kv.second.getType()));	// type
			RETURN_IF_FAILED(Variant::WRITE_FUNCS[kv.second.getType()](parcel, kv.second));
		}
		
		return NO_ERROR;
	}

	status_t Bundle::readFromParcelInner(const Parcel* parcel, size_t length)
	{
		/*
		 * Note: we don't actually use length for anything other than an empty Bundle
		 * check, since we do not actually need to copy in an entire Parcel, unlike in the Java
		 * implementation.
		 */
		if (length == 0) {
			// Empty Bundle or end of data.
			return NO_ERROR;
		}

		int32_t magic;
		RETURN_IF_FAILED(parcel->readInt32(&magic));
		if (magic != BUNDLE_MAGIC && magic != BUNDLE_MAGIC_NATIVE) {
			ALOGE("Bad magic number for Bundle: 0x%08x", magic);
			return BAD_VALUE;
		}

		/*
		 * To keep this implementation in sync with unparcel() in
		 * frameworks/base/core/java/android/os/BaseBundle.java, the number of
		 * key-value pairs must be read from the parcel before reading the key-value
		 * pairs themselves.
		 */
		int32_t numEntries;
		RETURN_IF_FAILED(parcel->readInt32(&numEntries));

		for (; numEntries > 0; --numEntries) {
			std::string key;
			int32_t type;
			RETURN_IF_FAILED(parcel->readString(&key));
			RETURN_IF_FAILED(parcel->readInt32(&type));	// type
			if (type < Variant::TYPE_INT8 || type >= Variant::TYPE_MAX)
			{
				return BAD_TYPE;
			}
			RETURN_IF_FAILED(Variant::READ_FUNCS[type](mMap[key], parcel));
		}

		return NO_ERROR;
	}
	
	
	const Bundle& Bundle::getEmpty()
	{
		static Bundle* instance = new Bundle();
		return *instance;
	}

} // namespace ipc


namespace ipc
{
	// (非框架设计者主要是使用这些常量作为KEY)
	static const std::string KEY_ID 			= "_id";
	static const std::string KEY_CLIENT 		= "_client";
	static const std::string KEY_OBSERVER 		= "_observer";		
	static const std::string KEY_VALUE  		= "_value"; // 如果Bundle只额外附加一个参数，请使用这个key 
		
	
	// 设置/获取id
	std::string BundleHelper::getId(const Bundle& data)
	{
		return data.getString(KEY_ID, EMPTY_STR);
	}
	
	void BundleHelper::setId(Bundle& data, const std::string& id)
	{
		data.setString(KEY_ID, id);
	}
		
	// 获取客户端名称
	std::string BundleHelper::getClientName(const Bundle& data)
	{
		return data.getString(KEY_CLIENT, EMPTY_STR);
	}
	
	
	// 设置值 -----------------------------------------------------------------------------

	void BundleHelper::setInt8(Bundle& data, const int8_t value)
	{
		data.setInt8(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt16(Bundle& data, const int16_t value)
	{
		data.setInt16(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt32(Bundle& data, const int32_t value)
	{
		data.setInt32(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt64(Bundle& data, const int64_t value)
	{
		data.setInt64(KEY_VALUE, value);
	}
	
	void BundleHelper::setFloat(Bundle& data, const float value)
	{
		data.setFloat(KEY_VALUE, value);
	}
	
	void BundleHelper::setDouble(Bundle& data, const double value)
	{
		data.setDouble(KEY_VALUE, value);
	}
	
	void BundleHelper::setString(Bundle& data, const std::string& value)
	{
		data.setString(KEY_VALUE, value);
	}
		
	// -----------------------------------------------------------------------------
	
	void BundleHelper::setInt8Vector(Bundle& data, const std::vector<int8_t>& value)
	{
		data.setInt8Vector(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt16Vector(Bundle& data, const std::vector<int16_t>& value)
	{
		data.setInt16Vector(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt32Vector(Bundle& data, const std::vector<int32_t>& value)
	{
		data.setInt32Vector(KEY_VALUE, value);
	}
	
	void BundleHelper::setInt64Vector(Bundle& data, const std::vector<int64_t>& value)
	{
		data.setInt64Vector(KEY_VALUE, value);
	}
	
	void BundleHelper::setFloatVector(Bundle& data, const std::vector<float>& value)
	{
		data.setFloatVector(KEY_VALUE, value);
	}
	
	void BundleHelper::setDoubleVector(Bundle& data, const std::vector<double>& value)
	{
		data.setDoubleVector(KEY_VALUE, value);
	}
	
	void BundleHelper::setStringVector(Bundle& data, const std::vector<std::string>& value)
	{
		data.setStringVector(KEY_VALUE, value);
	}
	

	// 设置(右值版本,防止大规模变量拷贝) ---------------------------------------------------------------------------------------;

	void BundleHelper::setInt8Vector(Bundle& data, std::vector<int8_t>&& value) const
	{
		data.setInt8Vector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setInt16Vector(Bundle& data, std::vector<int16_t>&& value) const
	{
		data.setInt16Vector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setInt32Vector(Bundle& data, std::vector<int32_t>&& value) const
	{
		data.setInt32Vector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setInt64Vector(Bundle& data, std::vector<int64_t>&& value) const
	{
		data.setInt64Vector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setFloatVector(Bundle& data, std::vector<float>&& value) const
	{
		data.setFloatVector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setDoubleVector(Bundle& data, std::vector<double>&& value) const
	{
		data.setDoubleVector(KEY_VALUE, std::move(value));
	}
	
	void BundleHelper::setStringVector(Bundle& data, std::vector<std::string>&& value) const
	{
		data.setStringVector(KEY_VALUE, std::move(value));
	}
	

	// 获取 -------------------------------------------------------------------------------------------------

	int8_t BundleHelper::getInt8(const Bundle& data, const int8_t defaultValue) const
	{
		return data.getInt8(KEY_VALUE, defaultValue);
	}
	
	int16_t BundleHelper::getInt16(const Bundle& data, const int16_t defaultValue) const
	{
		return data.getInt16(KEY_VALUE, defaultValue);
	}
	
	int32_t BundleHelper::getInt32(const Bundle& data, const int32_t defaultValue) const
	{
		return data.getInt32(KEY_VALUE, defaultValue);
	}
	
	int64_t BundleHelper::getInt64(const Bundle& data, const int64_t defaultValue) const
	{
		return data.getInt64(KEY_VALUE, defaultValue);
	}
	
	float BundleHelper::getFloat(const Bundle& data, const float defaultValue) const
	{
		return data.getFloat(KEY_VALUE, defaultValue);
	}
	
	double BundleHelper::getDouble(const Bundle& data, const double defaultValue) const
	{
		return data.getDouble(KEY_VALUE, defaultValue);
	}
	
	std::string BundleHelper::getString(const Bundle& data, const std::string& defaultValue) const
	{
		return data.getString(KEY_VALUE, defaultValue);
	}
	
	// ------------------------------------------------------------------------------------------------
	
	std::vector<int8_t> BundleHelper::getInt8Vector(const Bundle& data) const
	{
		return data.getInt8Vector(KEY_VALUE);
	}
	
	std::vector<int16_t> BundleHelper::getInt16Vector(const Bundle& data) const
	{
		return data.getInt16Vector(KEY_VALUE);
	}
	
	std::vector<int32_t> BundleHelper::getInt32Vector(const Bundle& data) const
	{
		return data.getInt32Vector(KEY_VALUE);
	}
	
	std::vector<int64_t> BundleHelper::getInt64Vector(const Bundle& data) const
	{
		return data.getInt64Vector(KEY_VALUE);
	}
	
	std::vector<float> BundleHelper::getFloatVector(const Bundle& data) const
	{
		return data.getFloatVector(KEY_VALUE);
	}
	
	std::vector<double> BundleHelper::getDoubleVector(const Bundle& data) const
	{
		return data.getDoubleVector(KEY_VALUE);
	}
	
	std::vector<std::string> BundleHelper::getStringVector(const Bundle& data) const
	{
		return data.getStringVector(KEY_VALUE);
	}
	
	// ---------------------------------------------------------------------------------------
				
	const std::vector<int8_t>& BundleHelper::getInt8VectorRef(const Bundle& data) const
	{
		return data.getInt8VectorRef(KEY_VALUE);
	}
	
	const std::vector<int16_t>& BundleHelper::getInt16VectorRef(const Bundle& data) const
	{
		return data.getInt16VectorRef(KEY_VALUE);
	}
	
	const std::vector<int32_t>& BundleHelper::getInt32VectorRef(const Bundle& data) const
	{
		return data.getInt32VectorRef(KEY_VALUE);
	}
	
	const std::vector<int64_t>& BundleHelper::getInt64VectorRef(const Bundle& data) const
	{
		return data.getInt64VectorRef(KEY_VALUE);
	}
	
	const std::vector<float>& BundleHelper::getFloatVectorRef(const Bundle& data) const
	{
		return data.getFloatVectorRef(KEY_VALUE);
	}
	
	const std::vector<double>& BundleHelper::getDoubleVectorRef(const Bundle& data) const
	{
		return data.getDoubleVectorRef(KEY_VALUE);
	}
	
	const std::vector<std::string>& BundleHelper::getStringVectorRef(const Bundle& data) const
	{
		return data.getStringVectorRef(KEY_VALUE);
	}
	
} // namespace ipc







// -----------------------------------------------------------------

namespace ipc
{	
	static std::thread sThread;	
	
	static void prvGMainLoop()
	{
		GMainLoop* loop = g_main_loop_new(NULL, FALSE);
		g_main_loop_run(loop);
		g_main_loop_unref(loop);
	}

	void IpcKit::init()
	{
		if (!sThread.joinable())
		{
			sThread = std::thread(prvGMainLoop);
		}
	}
	
	void IpcKit::join()
	{
		sThread.join();
	}
		
	// 每个数据都应该包含一个id
	const std::string& IpcKit::getKeyId()
	{
		return KEY_ID;
	}
	
	// 这个用于告诉服务器是哪个客户端发送过来的消息
	const std::string& IpcKit::getKeyClient()
	{
		return KEY_CLIENT;
	}
	
	// 告诉客户端哪个监听器应该响应
	const std::string& IpcKit::getKeyObserver()
	{
		return KEY_OBSERVER;
	}
	
	// 通常数据只附加一个参数
	const std::string& IpcKit::getKeyValue()
	{
		return KEY_VALUE;
	}	
	
	// 空字符串
	const std::string& IpcKit::getEmptyStr()
	{
		return EMPTY_STR;
	}
	
	// -----------------------------------------------------------------------------
	
	// 设置id
	void IpcKit::setId(Bundle& data, const std::string& id)
	{
		data.setString(KEY_ID, id);
	}
	
	// 获取id
	std::string IpcKit::getId(const Bundle& data)
	{
		return data.getString(KEY_ID, EMPTY_STR);
	}
	
	// 后去客户端名称
	std::string IpcKit::getClientName(const Bundle& data)
	{
		return data.getString(KEY_CLIENT, EMPTY_STR);
	}

	// 获取服务名
	std::string IpcKit::getServiceNameFromId(const std::string& id)
	{
		size_t pos = id.find('.');
		if (pos != std::string::npos)
		{
			return id.substr(0, pos);
		}
		return std::string();
	}
	
	// 获取服务名
	std::string IpcKit::getServiceNameFromId(std::string&& _id)
	{
		std::string id = std::move(_id);
		return getServiceNameFromId(id);
	}	
} // namespace ipc





// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------
// 序列化相关
// ----------------------------------------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------------------------------------



//#include <stdio.h>
#define ALOGV(...) //{printf(__VA_ARGS__); printf("\n");}
#define LOG_ALLOC(...) // {printf(__VA_ARGS__); printf("\n");}

// 4字节对齐
#define PAD_SIZE_UNSAFE(s) (((s) + 3) & ~3)

#define SIZE_T_MAX INT32_MAX
#define COMPILE_TIME_ASSERT_FUNCTION_SCOPE(x)

static size_t pad_size(size_t s)
{
    if (s > (SIZE_T_MAX - 3)) {
        abort();
    }
    return PAD_SIZE_UNSAFE(s);
}



namespace ipc 
{

	// 构造/析构函数相关 ------------------------------------------------------------------------------------------

	Parcel::Parcel()
	{
		LOG_ALLOC("Parcel@%p: constructing", this);
		initState();
	}
	
	Parcel::Parcel(const void* data, size_t len)
	{
		initState();
		write(data, len);
	}

	Parcel::~Parcel()
	{
		freeDataNoInit();
		LOG_ALLOC("Parcel@%p: destroyed", this);
	}

	// 成员变量相关 ----------------------------------------------------------------------------------------------

	const uint8_t* Parcel::data() const
	{
		return mData;
	}

	size_t Parcel::dataSize() const
	{
		return (mDataSize > mDataPos ? mDataSize : mDataPos);
	}

	size_t Parcel::dataAvail() const
	{
		size_t result = dataSize() - dataPosition();
		if (result > INT32_MAX) {
			abort();
		}
		return result;
	}

	size_t Parcel::dataPosition() const
	{
		return mDataPos;
	}

	size_t Parcel::dataCapacity() const
	{
		return mDataCapacity;
	}

	status_t Parcel::setDataSize(size_t size)
	{
		if (size > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		status_t err;
		err = continueWrite(size);
		if (err == NO_ERROR) {
			mDataSize = size;
			ALOGV("setDataSize Setting data size of Parcel@%p to %zu", this, mDataSize);
		}
		return err;
	}

	void Parcel::setDataPosition(size_t pos) const
	{
		if (pos > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			abort();
		}

		mDataPos = pos;
	}

	status_t Parcel::setDataCapacity(size_t size)
	{
		if (size > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		if (size > mDataCapacity)
			return continueWrite(size);
		return NO_ERROR;
	}

	void Parcel::freeData()
	{
		freeDataNoInit();
		initState();
	}

	// 写入操作相关 ----------------------------------------------------------------------------------------------

	namespace 
	{

		template <typename T>
		status_t writeByteVectorInternal(Parcel* parcel, const std::vector<T>& val)
		{
			status_t status;
			if (val.size() > (size_t)std::numeric_limits<int32_t>::max()) {
				status = BAD_VALUE;
				return status;
			}

			int32_t size = val.size() * sizeof(T);
			status = parcel->writeInt32(size);
			if (status != OK) {
				return status;
			}

			void* data = parcel->writeInplace(size);
			if (!data) {
				status = BAD_VALUE;
				return status;
			}

			memcpy(data, val.data(), size);
			return status;
		}

	} // namespace

	status_t Parcel::write(const void* data, size_t len)
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		void* const d = writeInplace(len);
		if (d) {
			memcpy(d, data, len);
			return NO_ERROR;
		}
		return mError;
	}

	// 设置下次读写位置，返回本次读写位置
	void* Parcel::writeInplace(size_t len)
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return NULL;
		}

		const size_t padded = pad_size(len);

		// sanity check for integer overflow
		if (mDataPos + padded < mDataPos) {
			return NULL;
		}

		if ((mDataPos + padded) <= mDataCapacity) {
		restart_write:
			//printf("Writing %ld bytes, padded to %ld\n", len, padded);
			uint8_t* const data = mData + mDataPos;

			// Need to pad at end?
			if (padded != len) {
	#if defined(BIG_ENDIAN) && BIG_ENDIAN
				static const uint32_t mask[4] = {
					0x00000000, 0xffffff00, 0xffff0000, 0xff000000
				};
	#else
				static const uint32_t mask[4] = {
					0x00000000, 0x00ffffff, 0x0000ffff, 0x000000ff
				};
	#endif
				//printf("Applying pad mask: %p to %p\n", (void*)mask[padded-len],
				//    *reinterpret_cast<void**>(data+padded-4));
				// padded != len
				// eg. len=3, padded=4, finally  value &= 0x00ffffff
				*reinterpret_cast<uint32_t*>(data + padded - 4) &= mask[padded - len];
			}

			finishWrite(padded);
			return data;
		}

		status_t err = growData(padded);
		if (err == NO_ERROR)
			goto restart_write;
		return NULL;
	}

	// ----------------------

	status_t Parcel::writeInt8(const int8_t val)
	{
		return writeAligned((const int32_t)val);
	}

	status_t Parcel::writeInt16(const int16_t val)
	{
		return writeAligned((const int32_t)val);
	}

	status_t Parcel::writeInt32(const int32_t val)
	{
		return writeAligned(val);
	}

	status_t Parcel::writeInt64(const int64_t val)
	{
		return writeAligned(val);
	}

	status_t Parcel::writeFloat(const float val)
	{
		return writeAligned(val);
	}

	status_t Parcel::writeDouble(const double val)
	{
		return writeAligned(val);
	}

	status_t Parcel::writeString(const std::string& str)
	{
		status_t err = writeInt32(str.length());
		if (err == NO_ERROR) {
			uint8_t* data = (uint8_t*)writeInplace(str.length());
			if (data) {
				memcpy(data, str.c_str(), str.length());
				return NO_ERROR;
			}
			err = mError;
		}
		return err;
	}

	status_t Parcel::writeInt8Vector(const std::vector<int8_t>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeInt16Vector(const std::vector<int16_t>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeInt32Vector(const std::vector<int32_t>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeInt64Vector(const std::vector<int64_t>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeFloatVector(const std::vector<float>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeDoubleVector(const std::vector<double>& val)
	{
		return writeByteVectorInternal(this, val);
	}

	status_t Parcel::writeStringVector(const std::vector<std::string>& val)
	{
		if (val.size() > (size_t)std::numeric_limits<int32_t>::max()) {
			return BAD_VALUE;
		}

		status_t status = this->writeInt32(static_cast<int32_t>(val.size()));

		if (status != OK) {
			return status;
		}

		for (const auto& item : val) {
			status = writeString(item);

			if (status != OK) {
				return status;
			}
		}

		return OK;
	}

// 读取操作相关 ----------------------------------------------------------------------------------------------

	namespace 
	{
		template <typename T>
		status_t readByteVectorInternal(const Parcel* parcel,
			std::vector<T>* val)
		{
			val->clear();

			int32_t size;
			status_t status = parcel->readInt32(&size);

			if (status != OK) {
				return status;
			}

			if (size < 0) {
				status = UNEXPECTED_NULL;
				return status;
			}
			if (size_t(size) > parcel->dataAvail()) {
				status = BAD_VALUE;
				return status;
			}

			T* data = const_cast<T*>(reinterpret_cast<const T*>(parcel->readInplace(size)));
			if (!data) {
				status = BAD_VALUE;
				return status;
			}
			val->reserve(size);
			val->insert(val->end(), data, data + size / sizeof(T));

			return status;
		}

	} // namespace

	status_t Parcel::read(void* outData, size_t len) const
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		if ((mDataPos + pad_size(len)) >= mDataPos // 没溢出
			&& (mDataPos + pad_size(len)) <= mDataSize // 足够数据
			&& len <= pad_size(len)) { // 没溢出
			memcpy(outData, mData + mDataPos, len);
			mDataPos += pad_size(len);
			ALOGV("read Setting data pos of Parcel@%p to %zu", this, mDataPos);
			return NO_ERROR;
		}
		return NOT_ENOUGH_DATA;
	}

	// 设置下次读写位置，返回本次读写位置
	const void* Parcel::readInplace(size_t len) const
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return NULL;
		}

		if ((mDataPos + pad_size(len)) >= mDataPos // 没溢出
			&& (mDataPos + pad_size(len)) <= mDataSize // 足够数据
			&& len <= pad_size(len)) { // 没溢出
			const void* data = mData + mDataPos;
			mDataPos += pad_size(len);
			ALOGV("readInplace Setting data pos of Parcel@%p to %zu", this, mDataPos);
			return data;
		}
		return NULL;
	}

	// ----------------------

	status_t Parcel::readInt8(int8_t* pArg) const
	{
		int32_t tmp = 0;
		status_t ret = readInt32(&tmp);
		*pArg = int8_t(tmp);
		return ret;
	}

	status_t Parcel::readInt16(int16_t* pArg) const
	{
		int32_t tmp = 0;
		status_t ret = readInt32(&tmp);
		*pArg = int16_t(tmp);
		return ret;
	}

	status_t Parcel::readInt32(int32_t* pArg) const
	{
		return readAligned(pArg);
	}

	status_t Parcel::readInt64(int64_t* pArg) const
	{
		return readAligned(pArg);
	}

	status_t Parcel::readFloat(float* pArg) const
	{
		return readAligned(pArg);
	}

	status_t Parcel::readDouble(double* pArg) const
	{
		return readAligned(pArg);
	}

	status_t Parcel::readString(std::string* pArg) const
	{
		int32_t size = readInt32();
		if (size >= 0 && size < INT32_MAX) {
			const char* data = reinterpret_cast<const char*>(readInplace(size));
			if (data) {
				pArg->assign(data, size);
				return NO_ERROR;
			}
		}
		*pArg = EMPTY_STR;
		return UNEXPECTED_NULL;
	}

	status_t Parcel::readInt8Vector(std::vector<int8_t>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readInt16Vector(std::vector<int16_t>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readInt32Vector(std::vector<int32_t>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readInt64Vector(std::vector<int64_t>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readFloatVector(std::vector<float>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readDoubleVector(std::vector<double>* val) const
	{
		return readByteVectorInternal(this, val);
	}

	status_t Parcel::readStringVector(std::vector<std::string>* val) const
	{
		int32_t size;
		status_t status = this->readInt32(&size);

		if (status != OK) {
			return status;
		}

		if (size < 0) {
			return UNEXPECTED_NULL;
		}

		if (val->max_size() < static_cast<size_t>(size)) {
			return NO_MEMORY;
		}

		val->resize(static_cast<size_t>(size));

		if (val->size() < static_cast<size_t>(size)) {
			return NO_MEMORY;
		}

		for (auto& v : *val) {
			status = readString(&v);

			if (status != OK) {
				return status;
			}
		}

		return OK;
	}

	int32_t Parcel::readInt32() const
	{
		return readAligned<int32_t>();
	}

	// 辅助函数相关 ----------------------------------------------------------------------------------------------

	void Parcel::initState()
	{
		LOG_ALLOC("Parcel@%p: initState", this);
		mError = NO_ERROR;
		mData = 0;
		mDataSize = 0;
		mDataCapacity = 0;
		mDataPos = 0;
		ALOGV("initState Setting data size of Parcel@%p to %zu", this, mDataSize);
		ALOGV("initState Setting data pos of Parcel@%p to %zu", this, mDataPos);
	}

	void Parcel::freeDataNoInit()
	{
		if (mData) {
			LOG_ALLOC("Parcel@%p: freeing with %zu capacity", this, mDataCapacity);
			free(mData);
		}
	}

	// ---------------------

	status_t Parcel::finishWrite(size_t len)
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		//printf("Finish write of %d\n", len);
		mDataPos += len;
		ALOGV("finishWrite Setting data pos of Parcel@%p to %zu", this, mDataPos);
		if (mDataPos > mDataSize) {
			mDataSize = mDataPos;
			ALOGV("finishWrite Setting data size of Parcel@%p to %zu", this, mDataSize);
		}
		//printf("New pos=%d, size=%d\n", mDataPos, mDataSize);
		return NO_ERROR;
	}

	status_t Parcel::continueWrite(size_t desired)
	{
		if (desired > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		if (mData) {
			// We own the data, so we can just do a realloc().
			if (desired > mDataCapacity) {
				uint8_t* data = (uint8_t*)realloc(mData, desired);
				if (data) {
					LOG_ALLOC("Parcel@%p: continue from %zu to %zu capacity", this, mDataCapacity, desired);
					mData = data;
					mDataCapacity = desired;
				} else {
					mError = NO_MEMORY;
					return NO_MEMORY;
				}
			} else {
				if (mDataSize > desired) {
					mDataSize = desired;
					ALOGV("continueWrite Setting data size of Parcel@%p to %zu", this, mDataSize);
				}
				if (mDataPos > desired) {
					mDataPos = desired;
					ALOGV("continueWrite Setting data pos of Parcel@%p to %zu", this, mDataPos);
				}
			}

		} else {
			// This is the first data.  Easy!
			uint8_t* data = (uint8_t*)malloc(desired);
			if (!data) {
				mError = NO_MEMORY;
				return NO_MEMORY;
			}

			LOG_ALLOC("Parcel@%p: allocating with %zu capacity", this, desired);

			mData = data;
			mDataSize = mDataPos = 0;
			ALOGV("continueWrite Setting data size of Parcel@%p to %zu", this, mDataSize);
			ALOGV("continueWrite Setting data pos of Parcel@%p to %zu", this, mDataPos);
			mDataCapacity = desired;
		}

		return NO_ERROR;
	}

	status_t Parcel::growData(size_t len)
	{
		if (len > INT32_MAX) {
			// don't accept size_t values which may have come from an
			// inadvertent conversion from a negative int.
			return BAD_VALUE;
		}

		size_t newSize = ((mDataSize + len) * 3) / 2;
		// 避免频繁分配
		if (newSize < 128) {
			newSize = 128;
		}
		return (newSize <= mDataSize) // 溢出
			? (status_t)NO_MEMORY
			: continueWrite(newSize);
	}

	// ---------------------

	template <class T>
	status_t Parcel::writeAligned(T val)
	{
		COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));

		if ((mDataPos + sizeof(val)) <= mDataCapacity) {
		restart_write:
			*reinterpret_cast<T*>(mData + mDataPos) = val;
			return finishWrite(sizeof(val));
		}

		status_t err = growData(sizeof(val));
		if (err == NO_ERROR)
			goto restart_write;
		return err;
	}

	template <class T>
	status_t Parcel::readAligned(T* pArg) const
	{
		COMPILE_TIME_ASSERT_FUNCTION_SCOPE(PAD_SIZE_UNSAFE(sizeof(T)) == sizeof(T));

		if ((mDataPos + sizeof(T)) <= mDataSize) {
			const void* data = mData + mDataPos;
			mDataPos += sizeof(T);
			*pArg = *reinterpret_cast<const T*>(data);
			return NO_ERROR;
		} else {
			return NOT_ENOUGH_DATA;
		}
	}

	template <class T>
	T Parcel::readAligned() const
	{
		T result;
		if (readAligned(&result) != NO_ERROR) {
			result = 0;
		}

		return result;
	}

} // namespace ipc

